<template>
  <!-- 主体区域 -->
  <section id="app">
    <TodoHead @add="handleAdd"></TodoHead>
    <TodoMain @del="handleDel" :list="list"></TodoMain>
    <TodoFooter @click="handleClear" :list="list"></TodoFooter>
  </section>
</template>

<script>
import TodoHead from './components/TodoHead.vue'
import TodoMain from './components/TodoMain.vue'
import TodoFooter from './components/TodoFooter.vue'
export default {
  data() {
    return {
      list: JSON.parse(localStorage.getItem('list')) || [
        { id: 1, name: '打篮球' },
        { id: 2, name: '游泳' },
        { id: 3, name: '踢足球' }
      ]
    }
  },
  methods: {
    handleAdd(todoName) {
      this.list.unshift({
        id: +new Date(),
        name: todoName
      })
    },
    handleDel(id) {
      this.list = this.list.filter(item => item.id !== id)
    },
    handleClear() {
      this.list = []
    }
  },
  watch: {
    list: {
      deep: true,
      handler(newValue) {
        localStorage.setItem('list', JSON.stringify(newValue))
      }
    }
  },
  components: { 
    TodoFooter, 
    TodoHead, 
    TodoMain 
  }
}
</script>

<style></style>
